<template>
  <div class="login">
    <!-- 完成结构 -->
    <el-form ref="form" :model="form" :rules="rules" class="myform">
      <h3>iHRM 后台登录系统</h3>
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          prefix-icon="el-icon-user-solid"
          placeholder="请输入手机"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          show-password
          prefix-icon="el-icon-lock"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" @click="login" class="btn" type="primary"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import { loginApi } from '@/api/user.js'
// 导入辅助函数
import { mapActions } from 'vuex'
export default {
  data () {
    return {
      // 按钮的加载效果
      loading: false,
      form: {
        mobile: '13800000002', // 手机号
        password: '123456' // 密码
      },
      // 定义校验规则
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              // 定义手机号的正则
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              // 测试数据
              const bool = reg.test(value)
              bool ? callback() : callback(new Error('手机号不合法'))
            },
            trigger: 'blur'
          }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    ...mapActions({
      userLogin: 'user/login'
    }),
    login () {
      // 完成全局的表单校验
      this.$refs.form.validate(async result => {
        if (result) {
          try {
            // 将按钮设置为加载状态
            this.loading = true
            // 发送请求：借助 store/modules/user.js/actions/login
            await this.userLogin(this.form)
            // 将按钮设置为默认状态
            this.loading = false
            // 添加提示信息
            this.$message({
              message: '欢迎回家',
              type: 'success'
            })
            // 判断当前路径中是否有参数： _redirect
            const _redirect = this.$route.query._redirect
            if (_redirect) {
              // 跳转到 _redirect 对应的页面
              this.$router.push(_redirect)
            } else {
              this.$router.push('/')
            }
          } catch (error) {
            console.log(error.message)
          }
          // // 发送请求
          // const res = await loginApi(this.form)
          // // 接收响应
          // console.log(res)
        } else {
          console.log('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login {
  background-image: url('../../assets/common/login.jpg');
  background-position: center;
  height: 100%;
  .myform {
    width: 520px;
    height: 400px;
    padding: 36px;
    padding-top: 0px;
    box-sizing: border-box;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -70%);
    h3 {
      font-size: 48px;
      font-weight: 200;
      text-align: center;
      letter-spacing: 1px; // 设置文本之间的间隔
      color: #fff;
      line-height: 1;
    }
    // 设置输入框的高度
    input.el-input__inner {
      height: 50px;
      background-color: rgba(255, 255, 255, 0.7);
      border: none;
      color: #68b0fe;
      border-radius: 6px;
      padding-left: 40px;
    }
    i.el-input__icon {
      color: #666;
    }
    .btn {
      width: 100%;
      height: 60px;
      font-size: 24px;
      background-color: #407ffe;
      border-radius: 6px;
    }
    .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
